package edu.ynmd.bmi.service.impl;

import edu.ynmd.bmi.dao.AreaDao;
import edu.ynmd.bmi.dao.CityDao;
import edu.ynmd.bmi.dao.ProvinceDao;
import edu.ynmd.bmi.dao.StuinfoDao;
import edu.ynmd.bmi.model.Area;
import edu.ynmd.bmi.model.City;
import edu.ynmd.bmi.model.Province;
import edu.ynmd.bmi.model.Stuinfo;
import edu.ynmd.bmi.service.MangeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class ManageServiceImpl implements MangeService {

    @Autowired
    private StuinfoDao stuinfoDao;

    @Autowired
    private ProvinceDao provinceDao;

    @Autowired
    private CityDao cityDao;
    @Autowired
    private AreaDao areaDao;



    @Override
    public boolean addStuinfo(Stuinfo stuinfo) {
        try {
            stuinfoDao.save(stuinfo);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }


    }

    @Override
    public boolean deleteStuinfo(String stuinfoid) {

        try {
            stuinfoDao.deleteById(stuinfoid);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }



    }

    @Override
    public boolean updateStuinfo(Stuinfo stuinfo) {
        try {
            stuinfoDao.save(stuinfo);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }


    }

    @Override
    public Stuinfo getSingleStuinfo(String stuifnid) {

        try {
            Optional<Stuinfo> optionalstuinfo=stuinfoDao.findById(stuifnid);

            return optionalstuinfo.get();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }


    }

    @Override
    public List<Stuinfo> getStuinfoList() {
        return stuinfoDao.findAll();
    }


    @Override
    public String getProvinceNameByCode(String provincecode) {
        List<Province> pl=provinceDao.getProvinceByCode(provincecode);
        if(pl.size()>0){
            return pl.get(0).getName();
        }

        return "";
    }

    @Override
    public String getCityNameByCode(String cotycode) {
        List<City> cl= cityDao.getCitiesByCode(cotycode);
        if(cl.size()>0){
            return cl.get(0).getName();
        }
        return null;
    }

    @Override
    public String getAreaNameByCode(String areacode) {

        List<Area> al=areaDao.getAreaByCode(areacode);
        if(al.size()>0){
            return al.get(0).getName();
        }
        return null;
    }


    @Override
    public String getSexNameByCode(String sexcode) {

        int si=Integer.parseInt(sexcode);

        if(si%2==0){
            return "女";
        }
        else{
            return "男";
        }






    }
}